package com.app.twth.ui.putaway;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.app.twth.R;
import com.app.twth.databinding.ItemPutawayListBinding;
import com.app.twth.interfaces.MyOnClickListener;
import com.app.twth.interfaces.MyTwoStoryOnClickListener;
import com.app.twth.utils.DividerItemDecoration;
import com.app.twth.utils.Utils;

import java.util.ArrayList;
import java.util.List;


public class PutawayAdapter extends RecyclerView.Adapter<PutawayAdapter.MyViewHolder> {
    private Context mContext;
    private LayoutInflater mInflater;
    private List<Putatay> data = new ArrayList<>();
    private MyOnClickListener locationListener;
    private MyTwoStoryOnClickListener detailListener;

    public PutawayAdapter(Context mContext, List<Putatay> data, MyOnClickListener locationListener, MyTwoStoryOnClickListener detailListener) {
        this.mContext = mContext;
        this.data = data;
        this.mInflater = LayoutInflater.from(mContext);
        this.locationListener = locationListener;
        this.detailListener = detailListener;
    }

    public void setData(List<Putatay> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(ItemPutawayListBinding.inflate(mInflater, parent, false));
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.setData(position);
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ItemPutawayListBinding itemBinding;

        public MyViewHolder(ItemPutawayListBinding itemBinding) {
            super(itemBinding.getRoot());
            this.itemBinding = itemBinding;
        }

        public void setData(int position) {
            itemView.setTag(position);
            Putatay container = data.get(position);
            if (Utils.isEmpty(container.getTalliedDate())) {
                itemBinding.tvItem.setText(container.getContainerNo() + "");
            } else {
                itemBinding.tvItem.setText(container.getContainerNo() + " (" + container.getTalliedDate() + ")");
            }
            itemBinding.ivDown.setOnClickListener(v -> {
                locationListener.OnClick(position);
            });
            if (container.isShow()) {
                LinearLayoutManager layoutManager1 = new LinearLayoutManager(mContext);
                layoutManager1.setOrientation(LinearLayoutManager.VERTICAL);
                itemBinding.recyclerviewDetail.setLayoutManager(layoutManager1);
                //添加装饰类
                itemBinding.recyclerviewDetail.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL_LIST));
                itemBinding.recyclerviewDetail.setAdapter(new PutawayDetailAdapter(mContext, container.getContainerDetail(), new MyOnClickListener() {
                    @Override
                    public void OnClick(int position2) {
                        detailListener.OnClick(position, position2);
                    }
                }));

                itemBinding.recyclerviewDetail.setVisibility(View.VISIBLE);
                itemBinding.ivDown.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_arrow_up));
            } else {
                itemBinding.recyclerviewDetail.setVisibility(View.GONE);
                itemBinding.ivDown.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_arrow_down));
            }
        }
    }

}
